package entityopinions;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;

public class SearchEngineCounts {
	
	private static String appIDBing = "C95FBF000810BE5EB21C73009C32882962C22E12";
	
	private static String queryURL = "http://api.bing.net/xml.aspx?AppId=" + appIDBing + "&Sources=web+spell&Web.Count=1&Query=";
	
	public static long countResults ( String word ) {
		try {
			String location = queryURL + URLEncoder.encode(word);
			URL url = new URL(location);
			URLConnection conn = url.openConnection ();
			BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
			StringBuffer sb = new StringBuffer();
			String line;
			while ((line = rd.readLine()) != null) sb.append(line);
			rd.close();
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
	        DocumentBuilder db = dbf.newDocumentBuilder();
	        InputSource is = new InputSource();
	        is.setCharacterStream(new StringReader(sb.toString()));
	        Document doc = db.parse(is);
	        XPath xpath = XPathFactory.newInstance().newXPath();
	        XPathExpression expr = xpath.compile("//Total/text()");
	        return new Long(expr.evaluate(doc));
		} catch ( Exception e ) { return -1; }
	}

	public static long countNearResults ( String word1, String word2, int sep ) {
		try {
			String location = queryURL + URLEncoder.encode(word1) + "+near:" + sep + "+" + URLEncoder.encode(word2);
			URL url = new URL(location);
			URLConnection conn = url.openConnection ();
			BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
			StringBuffer sb = new StringBuffer();
			String line;
			while ((line = rd.readLine()) != null) sb.append(line);
			rd.close();
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
	        DocumentBuilder db = dbf.newDocumentBuilder();
	        InputSource is = new InputSource();
	        is.setCharacterStream(new StringReader(sb.toString()));
	        Document doc = db.parse(is);
	        XPath xpath = XPathFactory.newInstance().newXPath();
	        XPathExpression expr = xpath.compile("//Total/text()");
	        return new Long(expr.evaluate(doc));
		} catch ( Exception e ) { return -1; }
	}


        //teste
        public static void main(String[] args){

            SearchEngineCounts sec = new SearchEngineCounts();
            long countsGoodExcellent, countsExcellent, countsGood, countsPoor, countsGoodPoor;
            double so;

            countsGoodExcellent = sec.countNearResults("good", "excellent", 10);
            countsGoodPoor = sec.countNearResults("good", "poor", 10);
            countsExcellent = sec.countResults("excellent");
            countsGood = sec.countResults("good");
            countsPoor = sec.countResults("poor");

            System.out.println("A palavra good com a palavra excelente: " + countsGoodExcellent);
            System.out.println("A palavra excelente: " + countsExcellent);
            System.out.println("A palavra good com a palavra poor: " + countsGoodPoor);
            System.out.println("A palavra poor: " + countsPoor);


            so = Math.log((double)(countsGoodExcellent * countsPoor)/(countsGoodPoor * countsExcellent))/Math.log(2);

            System.out.println("Orientação semantica de good: " + so);




            /*
            System.out.println("A palavra good com a palavra excelente: " + countsExcellent);
            System.out.println("A palavra good com a palavra poor: " + countsPoor);
            System.out.println("PMI good: " + countsExcellent/(sec.countResults("good")*sec.countResults("excellent")));
            countsExcellent = sec.countNearResults("bad", "excellent", 10);
            countsPoor = sec.countNearResults("bad", "poor", 10);
            System.out.println("A palavra bad com a palavra excelente: " + countsExcellent);
            System.out.println("A palavra bad com a palavra poor: " + countsPoor);
*/
        }

}
